<?php
include "QuickCSVImport.php"; // Have you class seperate from your working code.
$path=getcwd()."\\";
//connect to database
$_host      =(isset($_POST["_host"]))?$_POST["_host"]:"localhost";
$_host      =($_host!="")?$_host:"localhost";
$_user      =(isset($_POST["_user"]))?$_POST["_user"]:"root";
$_user      =($_user!="")?$_user:"root";
$_pass      =(isset($_POST["_pass"]))?$_POST["_pass"]:"";
$_data      =(isset($_POST["_data"]))?$_POST["_data"]:"";
$_tablecols = isset($_POST["_tablecols"])?$_POST["_tablecols"]:null;
mysql_connect($_host, $_user, $_pass);

mysql_select_db($_data); //your database
 
$csv = new Quick_CSV_import();
 
$arr_encodings = $csv->get_encodings(); //take possible encodings list
$arr_encodings["default"] = "[default database encoding]"; //set a default (when the default database encoding should be used)
 
if(!isset($_POST["encoding"]))
  $_POST["encoding"] = "default"; //set default encoding for the first page show (no POST vars)
$file_source=$_POST["_csvfile"];
$_table=$_POST["_table"];
if(isset($_POST["_upload"]) && ""!=$_POST["_upload"]) //form was submitted
{
  if ($_FILES["file"]["size"] < 80000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    $csv->error.= "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    $csv->error.= "Upload: " . $_FILES["file_source"]["name"] . "<br />";
    $csv->error.= "Type: " . $_FILES["file_source"]["type"] . "<br />";
    $csv->error.= "Size: " . ($_FILES["file_source"]["size"] / 1024) . " Kb<br />";
    $csv->error.= "Temp file: " . $_FILES["file_source"]["tmp_name"] . "<br />";
    $_fn=date("dmYHis_").$_FILES["file_source"]["name"];
    if (file_exists("CSVFile/" . $_fn))
      {
      $csv->error.= $_fn . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file_source"]["tmp_name"],
      "CSVFile/" . $_fn);
      $file_source="CSVFile/".$_fn;//$_FILES['file_source']["name"];
      }
    }
  }else{
    $csv->error.= "Invalid file";
  }
}
if(isset($_POST["Go"]) && ""!=$_POST["Go"]){ //form was submitted
  $csv->file_name = $path.$_POST["_csvfile"];//$_FILES['file_source']["tmp_name"];
  $csv->table_cols=array();
  $csv->table_cols=$_tablecols;
  $csv->use_csv_header = isset($_POST["use_csv_header"]);
  $csv->field_separate_char = $_POST["field_separate_char"][0];
  $csv->field_enclose_char = $_POST["field_enclose_char"][0];
  $csv->field_escape_char = $_POST["field_escape_char"][0];
  $csv->encoding = $_POST["encoding"];
  //start import now
  $csv->import(isset($_table)?$_table:null);
}
else
  $_POST["use_csv_header"] = 1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
    <title>Quick CSV import</title>
  <style>
  .edt
  {
    background:#ffffff; 
    border:3px double #aaaaaa; 
    -moz-border-left-colors:  #aaaaaa #ffffff #aaaaaa; 
    -moz-border-right-colors: #aaaaaa #ffffff #aaaaaa; 
    -moz-border-top-colors:   #aaaaaa #ffffff #aaaaaa; 
    -moz-border-bottom-colors:#aaaaaa #ffffff #aaaaaa; 
    width: 350px;
  }
  .edt_30
  {
    background:#ffffff; 
    border:3px double #aaaaaa; 
    font-family: Courier;
    -moz-border-left-colors:  #aaaaaa #ffffff #aaaaaa; 
    -moz-border-right-colors: #aaaaaa #ffffff #aaaaaa; 
    -moz-border-top-colors:   #aaaaaa #ffffff #aaaaaa; 
    -moz-border-bottom-colors:#aaaaaa #ffffff #aaaaaa; 
    width: 30px;
  }
  </style>
</head>
 
<body bgcolor="#f2f2f2">
  <h2 align="center">Quick CSV import</h2>
  <form method="post" enctype="multipart/form-data">
    <table border="0" align="center">
      <tr>
          <td>Upload CSV file to import:</td>
          <td rowspan="30" width="10px">&nbsp;</td>
          <td><input type="file" name="file_source" id="file_source" class="edt" value=""></td>
      </tr>
      <tr>
          <td colspan="3" align="center"><input type="Submit" name="_upload" value="Upload" onclick=" var s = document.getElementById('file_source'); if(null != s && '' == s.value) {alert('Define file name'); s.focus(); return false;}"></td>
      </tr>
      <tr>
          <td colspan="3">&nbsp;</td>
      </tr>
      <tr>
          <td>Source CSV File::</td>
          <td><input type="text" name="_csvfile" id="_csvfile" class="edt" value="<?=isset($file_source)?$file_source:""?>"></td>
      </tr>
      <tr>
          <td>Use CSV header:</td>
          <td><input type="checkbox" name="use_csv_header" id="use_csv_header" <?=(isset($_POST["use_csv_header"])?"checked":"")?>/></td>
      </tr>
      <tr>
          <td>Separate char:</td>
          <td><input type="text" name="field_separate_char" id="field_separate_char" class="edt_30"  maxlength="1" value="<?=(""!=$_POST["field_separate_char"] ? htmlspecialchars($_POST["field_separate_char"]) : ",")?>"/></td>
      </tr>
      <tr>
          <td>Enclose char:</td>
          <td><input type="text" name="field_enclose_char" id="field_enclose_char" class="edt_30"  maxlength="1" value="<?=(""!=$_POST["field_enclose_char"] ? htmlspecialchars($_POST["field_enclose_char"]) : htmlspecialchars("\""))?>"/></td>
      </tr>
      <tr>
          <td>Escape char:</td>
          <td><input type="text" name="field_escape_char" id="field_escape_char" class="edt_30"  maxlength="1" value="<?=(""!=$_POST["field_escape_char"] ? htmlspecialchars($_POST["field_escape_char"]) : "\\")?>"/></td>
      </tr>
      <tr>
          <td>Encoding:</td>
          <td>
          <select name="encoding" id="encoding" class="edt">
          <?
            if(!empty($arr_encodings))
              foreach($arr_encodings as $charset=>$description):
          ?>
            <option value="<?=$charset?>"<?=($charset == $_POST["encoding"] ? "selected=\"selected\"" : "")?>><?=$description?></option>
          <? endforeach;?>
          </select>
        </td>
      </tr>
      <tr>
          <td colspan="3">&nbsp;</td>
      </tr>
      <tr>
          <td colspan="3" align="center"></td>
      </tr>
      <tr>
          <td>Host:</td>
          <td><input type="text" name="_host" id="_host" class="edt" value="<?=isset($_host)?$_host:"localhost"?>"></td>
      </tr>
      <tr>
          <td>User Name:</td>
          <td><input type="text" name="_user" id="_user" class="edt" value="<?=isset($_user)?$_user:"root"?>"></td>
      </tr>
      <tr>
          <td>Pass Word:</td>
          <td><input type="text" name="_pass" id="_pass" class="edt" value="<?=isset($_pass)?$_pass:""?>"/></td>
      </tr>
      <tr>
          <td>Database:</td>
          <td><input type="text" name="_data" id="_data" class="edt" value="<?=isset($_data)?$_data:""?>"/></td>
      </tr>
      <tr>
          <td>Table:</td>
          <td><input type="text" name="_table" id="_table" class="edt" value="<?=$_table?>" /></td>
      </tr>
      <tr>
          <td colspan="3" align="center"><input type="Submit" name="_loadcols" value="Show Columns" onclick=" var s = document.getElementById('_csvfile'); if(null != s && '' == s.value) {alert('Define file name'); s.focus(); return false;}"></td>
      </tr>
      <tr>
          <td colspan="3">&nbsp;</td>
      </tr>
      
      <?
      if((isset($_POST["_loadcols"]) && ""!=$_POST["_loadcols"] && isset($_table) && $_table!="") || isset($_tablecols))
      if($file_source!=""){
            $csv->file_name = $path.$file_source;//$_FILES['file_source']["tmp_name"];
            $csv->use_csv_header = isset($_POST["use_csv_header"]);
            $csv->field_separate_char = $_POST["field_separate_char"][0];
            $csv->field_enclose_char = $_POST["field_enclose_char"][0];
            $csv->field_escape_char = $_POST["field_escape_char"][0];
            $csv->encoding = $_POST["encoding"];

            $csv_cols=$csv->get_csv_header_fields();
            $tbcols=  mysql_query("SHOW COLUMNS FROM `$_table`");
            $nr=mysql_num_rows($tbcols);
            $st=0;
            ?>
      <tr>
          <td>CSV Columns:</td>
          <td>Table Columns</td>
      </tr>
            <?
            foreach ($csv_cols as $col){
        ?>
      <tr>
          <td><div class="edt"><?=$col?></div></td>
          <td>
              <select name="_tablecols[]" class="edt">
                  <option value=""></option>
              <?
                for($i=0;$i<$nr;$i++){
                    if(!isset($_POST["_tablecols"]))
                        $sel=($st==$i)?" selected=1":"";
                    echo "<option ".((isset($_tablecols[$st])&&$_tablecols[$st]!="")?($_tablecols[$st]==mysql_result($tbcols, $i)?" selected=1":$sel):$sel).">".mysql_result($tbcols, $i)."</option>";
                }
                $st++;
          ?>
              </select>
              </td>
      </tr>   
        <?
            }
      }
      ?>
      
      <tr>
          <td colspan="3">&nbsp;</td>
      </tr>
      <tr>
          <td colspan="3" align="center"><input type="Submit" name="Go" value="Import it" onclick=" var s = document.getElementById('_csvfile'); if(null != s && '' == s.value) {alert('Define file name'); s.focus(); return false;}"></td>
      </tr>
    </table>
  </form>
<?=(!empty($csv->error) ? "<hr/>".$csv->error : "")?>
</body>
</html>